package com.zork.cmdb.webapi.mapper.cmdb;

import com.zork.cmdb.webapi.domain.dbo.ModuleDO;
import com.zork.cmdb.webapi.domain.dbo.ModuleRelationshipDO;
import com.zork.cmdb.webapi.domain.dbo.ModuleRelationshipGroupDO;
import com.zork.cmdb.webapi.domain.dbo.RelationshipTypeDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @author chenjh
 * @date 2018/5/24
 * @description
 */
@Mapper
public interface RelationshipInstanceMapper {

    @Select("SELECT [ModuleID]\n" +
            "      ,[ModuleName]\n" +
            "      ,[ModuleNum]\n" +
            "      ,[CategoryID]\n" +
            "      ,[ImageID]\n" +
            "      ,[IsUsable]\n" +
            "      ,[IsSystemDefined]\n" +
            "      ,[ParentID]\n" +
            "      ,[InstanceTableName]\n" +
            "      ,[CreateTime]\n" +
            "      ,[CreateUser] FROM [Module] ORDER BY [ModuleID]")
    List<ModuleDO> listModule();

    @Select("SELECT [RalationshipGroupID]\n" +
            "      ,[RalationshipGroupName]\n" +
            "      ,[IsSystemDefined]\n" +
            "      ,[Description]\n" +
            "      ,[CreateTime]\n" +
            "      ,[CreateUser]\n" +
            "  FROM [ModuleRalationshipGroup]")
    List<ModuleRelationshipGroupDO> listModuleRelationshipGroup();

    @Select("SELECT [RalationshipTypeID]\n" +
            "      ,[RalationshipTypeName]\n" +
            "      ,[IsSystemDefined]\n" +
            "      ,[Style]\n" +
            "  FROM [RalationshipType]")
    List<RelationshipTypeDO> listRelationshipType();

    @Select("SELECT [RalationshipID]\n" +
            "      ,[RalationshipTypeID]\n" +
            "      ,[SelectedModuleID]\n" +
            "      ,[RalationshipModuleID]\n" +
            "      ,[IsSystemDefined]\n" +
            "      ,[RalationshipLevel]\n" +
            "      ,[LimitCodition]\n" +
            "      ,[RalationshipGroupID]\n" +
            "      ,[SelectedModuleLevel]\n" +
            "      ,[RalationshipModuleLevel]\n" +
            "      ,[AttributesID]\n" +
            "  FROM [ModuleRalationship] \n" +
            "  WHERE [SelectedModuleID] = #{moduleID} OR [RalationshipModuleID] = #{moduleID}")
    List<ModuleRelationshipDO> getModuleRelationshipByModuleID(@Param("moduleID") Integer moduleID);

    @Select("${sql}")
    List<Map<String, Object>> listRelationshipInstance(@Param("sql") String sql);

    @Select("SELECT DISTINCT [RalationshipID]\n" +
            "FROM [InstanceRelationship] ir\n" +
            "WHERE (ir.[SelectedInstanceID] = #{moduleInstanceID}\n" +
            "AND EXISTS (\n" +
            "SELECT 1\n" +
            "FROM [ModuleRalationship] mr\n" +
            "WHERE mr.[RalationshipID] = ir.[RalationshipID]\n" +
            "AND mr.[SelectedModuleID] = #{moduleID}\n" +
            "))\n" +
            "OR (ir.[RelationInstanceID] = #{moduleInstanceID}\n" +
            "AND EXISTS (\n" +
            "SELECT 1\n" +
            "FROM [ModuleRalationship] mr\n" +
            "WHERE mr.[RalationshipID] = ir.[RalationshipID]\n" +
            "AND mr.[RalationshipModuleID] = #{moduleID}\n" +
            "))")
    List<Integer> listRalationshipID(@Param("moduleID") Integer moduleID,
                                     @Param("moduleInstanceID") Integer moduleInstanceID);
}
